<?php

namespace App\Validate\Web;

use App\Http\Models\ZtkTaskBlackUser;
use App\Http\Models\ZtkUser;
use App\Validate\BaseValidate;
use App\Http\Models\ZtkTask;
use App\Http\Models\Order;
use Illuminate\Support\Facades\Auth;

class TaskTakingValidate extends BaseValidate
{
    protected $rules = [];// 规则

    protected $message = [];// 自定义消息

    public function __construct($request)
    {
        parent::__construct($request);

        $this->rules = [
            'task_id' => 'required|numeric',
        ];
        $this->message = [
            'task_id.required' => '参数错误',
            'task_id.numeric' => '参数错误',
        ];
    }

    protected function customValidate()
    {
        $task_id = $this->requestData['task_id'];//提现金额

        $task = ZtkTask::find($task_id);
        if(!$task){
            $this->validator->errors()->add('no_task', '任务不存在');

            return false;
        }

        if($task->status != 1 ){
            $this->validator->errors()->add('status', '任务不是进行中，无法接单');

            return false;
        }

        if($task->stock_num < 1 ){
            $this->validator->errors()->add('stock_num', '任务已满额');

            return false;
        }

        if($task->user_id === Auth::id()){
            $this->validator->errors()->add('user_id', '自己发布的任务不能接单');

            return false;
        }

        $order = Order::where(['user_id' => Auth::id(), 'task_id' => $task_id])->first();
        if($order){
            $this->validator->errors()->add('order', '已经接过该任务了');

            return false;
        }

        $user = ZtkUser::find(Auth::id());
        if($user->status == 2){
            $this->validator->errors()->add('order', '账号被封不能接单');

            return false;
        }
        if($user->is_union_auth == 0){
            $this->validator->errors()->add('order', '未认证的联盟不能接单');

            return false;
        }

        $blackuser = ZtkTaskBlackUser::where(['black_user_id' => Auth::id(), 'task_id' => $task_id])->exists();
        if($blackuser){
            $this->validator->errors()->add('order', '发布人把您加入了黑名单，禁止您接此任务');

            return false;
        }

        $this->requestData['task'] = $task;

    }
}
